{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 本节课最终成果体验\n",
    "- 下载names.xlsx\n",
    "- 适当修改名字，添加少量名字，由于服务器资源限制，名字数不要超过10个\n",
    "- 上传修改好的文件\n",
    "- 运行下面的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from maioffice import word, excel\n",
    "\n",
    "#创建邀请函\n",
    "def invite(name):\n",
    "    file = 'template.docx'  \n",
    "    doc = word.load(file) \n",
    "    word.replace(doc, '<name>', name) \n",
    "    doc.save(name + '.docx') \n",
    "    \n",
    "#从Excel中读取名单，并为每个人创建邀请函\n",
    "wb = excel.load('names.xlsx')\n",
    "sheet = wb.active\n",
    "for row in sheet.rows:\n",
    "    cell = row[0]\n",
    "    name = cell.value\n",
    "    if cell.row > 1:\n",
    "        invite(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 自己动手开发\n",
    "- 下面已经给出了上节课最后的成果\n",
    "- 跟着老师补充完代码，实现从Excel中读取名单并自动生成请帖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from maioffice import word\n",
    "\n",
    "def invite(name):\n",
    "    file = 'template.docx'  \n",
    "    doc = word.load(file) \n",
    "    word.replace(doc, '<name>', name) \n",
    "    doc.save(name + '.docx') \n",
    "\n",
    "invite('张三')\n",
    "invite('李四')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 闯关练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### T31 打印电话号码\n",
    "- 修改下面的程序，在创建邀请函的同时打印出电话号码。\n",
    "- 注意只是打印电话号码，不需要添加到邀请函中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from maioffice import word, excel\n",
    "\n",
    "def invite(name):\n",
    "    file = 'template.docx'  \n",
    "    doc = word.load(file) \n",
    "    word.replace(doc, '<name>', name) \n",
    "    doc.save(name + '.docx') \n",
    "    \n",
    "wb = excel.load('names.xlsx')\n",
    "sheet = wb.active\n",
    "for row in sheet.rows:\n",
    "    cell = row[0]\n",
    "    name = cell.value\n",
    "    if cell.row > 1:\n",
    "        invite(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### T32 在邀请函中添加电话号码\n",
    "- 下载template.docx文件，并重命名为template5.docx\n",
    "- 修改template5.docx，在里面某个合适的地方添加PHONE作为电话号码的标签\n",
    "- 修改invite函数，使他能够同时替换名字和电话号码:\n",
    "    - invite函数需要再加1个参数phone\n",
    "    - 在invite代码中需要使用phone来替换PHONE标签\n",
    "- 修改excel相关的代码，同时传入名字和电话号码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from maioffice import word, excel\n",
    "\n",
    "def invite(name):\n",
    "    file = 'template.docx'  \n",
    "    doc = word.load(file) \n",
    "    word.replace(doc, '<name>', name) \n",
    "    doc.save(name + '.docx') \n",
    "    \n",
    "wb = excel.load('names.xlsx')\n",
    "sheet = wb.active\n",
    "for row in sheet.rows:\n",
    "    cell = row[0]\n",
    "    name = cell.value\n",
    "    if cell.row > 1:\n",
    "        invite(name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
